﻿using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using Sustainalytics.Synchronization.Validation.Validators;

namespace Sustainalytics.Synchronization.Validation.Log
{
    public interface ILogClient
    {
        void WriteAsync(IEnumerable<ValidationFailure> messages);
    }

    public class Log
    {
        private readonly IEnumerable<ILogClient> logClients;

        private readonly ConcurrentQueue<ValidationFailure> queueLog = new ConcurrentQueue<ValidationFailure>();

        public Log(IEnumerable<ILogClient> logClients)
        {
            this.logClients = logClients;
        }

        public void AddMessage(ValidationFailure message)
        {
            if (ValidationFailure.IsNullOrEmpty(message))
            {
                throw new ArgumentException("Argument is null or empty", "message");
            }

            queueLog.Enqueue(message);
        }

        public void AddMessages(IEnumerable<ValidationFailure> messages)
        {
            foreach (var message in messages)
            {
                queueLog.Enqueue(message);
            }
        }

        public void WriteLog()
        {
            foreach (var logClient in logClients)
            {
                logClient.WriteAsync(queueLog);
            }
        }
    }
}